﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;

namespace ZapHelper
{
    public class Product
    {

        #region Properties

        #region Product Name
        public static String ZAP_Name { get { return "PRODUCT_NAME"; } }
        public String Name { get; private set; }

        #endregion

        #region Product Url

        public static String ZAP_Url { get { return "PRODUCT_URL"; } }
        public String Url { get; private set; }

        #endregion

        #region Model

        public static String ZAP_Model { get { return "MODEL"; } }
        public String Model { get; private set; }

        #endregion

        #region Details

        public static String ZAP_Details { get { return "DETAILS"; } }
        public String Details { get; private set; }

        #endregion

        #region Catalog Number

        public static String ZAP_CatalogNumber { get { return "CATALOG_NUMBER"; } }
        public String CatalogNumber { get; private set; }

        #endregion

        #region Price Currency

        public static String ZAP_Currency { get { return "CURRENCY"; } }
        public String Currency { get; private set; }

        #endregion

        #region Price

        public static String ZAP_Price { get { return "PRICE"; } }
        public String Price { get; private set; }

        #endregion

        #region Shipment Cost

        public static String ZAP_ShipmentCost { get { return "SHIPMENT_COST"; } }
        public String ShipmentCost { get; private set; }

        #endregion

        #region Delivery Time

        public static String ZAP_DeliveryTime { get { return "DELIVERY_TIME"; } }
        public String DeliveryTime { get; private set; }

        #endregion

        #region Manufacturer
        public static String ZAP_Manufacturer { get { return "MANUFACTURER"; } }
        public String Manufacturer { get; private set; }

        #endregion

        #region Warrenty

        public static String ZAP_Warranty { get { return "WARRANTY"; } }
        public String Warrenty { get; private set; }

        #endregion

        #region Image

        public static String ZAP_Image { get { return "IMAGE"; } }
        public String Image { get; private set; }

        #endregion

        #region Tax
        public static String ZAP_Tax { get { return "TAX"; } }
        public String Tax { get; private set; }

        #endregion

        #region Type
        public static String ZAP_Type { get { return "PRODUCT_TYPE"; } }
        public String Type { get; private set; }

        #endregion

        #endregion

        #region Methods

        public Product(HtmlNode productNode)
        {

            if (productNode == null)
            {
                throw new ArgumentNullException("productNode");
            }

            /*
                parse the html node
                check that critical fields are 
                    + PRODUCT_NAME
                    + PRICE
                    + PRODUCT_URL
            */
            try
            {

                Url = Utils.GetHtmlNodeValue(productNode, ZAP_Url, true);
                Price = Utils.GetHtmlNodeValue(productNode, ZAP_Price, true);

                Model = Utils.GetHtmlNodeValue(productNode, ZAP_Model, false);
                Name = Utils.GetHtmlNodeValue(productNode, ZAP_Name, false);

                #region No Name Workaround

                // If Name is null and Model is not null
                if (String.IsNullOrWhiteSpace(Name) && String.IsNullOrWhiteSpace(Model))
                {
                    throw new ParseFailedException();
                }


                // If Name is null and Model is not null
                if (String.IsNullOrWhiteSpace(Name) && !String.IsNullOrWhiteSpace(Model))
                {
                    Name = Model;
                }

                #endregion

                CatalogNumber = Utils.GetHtmlNodeValue(productNode, ZAP_CatalogNumber, false);
                Details = Utils.GetHtmlNodeValue(productNode, ZAP_Details, false);
                ShipmentCost = Utils.GetHtmlNodeValue(productNode, ZAP_ShipmentCost, false);
                DeliveryTime = Utils.GetHtmlNodeValue(productNode, ZAP_DeliveryTime, false);
                Manufacturer = Utils.GetHtmlNodeValue(productNode, ZAP_Manufacturer, false);
                Warrenty = Utils.GetHtmlNodeValue(productNode, ZAP_Warranty, false);
                Type = Utils.GetHtmlNodeValue(productNode, ZAP_Type, false);
                Image = Utils.GetHtmlNodeValue(productNode, ZAP_Image, false);
                Currency = Utils.GetHtmlNodeValue(productNode, ZAP_Currency, false);
            }
            catch (Exception e)
            {
                throw new ParseFailedException("failed to parse product" , e);
            }
        }

        #endregion


    }
}
